package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.VisaListAdapter;
import com.kaisa.activity.bean.VisaListBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class VisaListActivty extends AppCompatActivity {

    private static final String PATH_VISALIST = "http://ws.caissa.com.cn/cms/app/VidaHandle.ashx?";
    private String countryID;

    @ViewInject(R.id.iv_visalist)
    private ImageView img;
    @ViewInject(R.id.lv_visalist)
    private ListView visaLV;
    private List<VisaListBean> list = new ArrayList<VisaListBean>();

    private VisaListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visalist);
        x.view().inject(this);

        Intent intent = getIntent();
        countryID = intent.getStringExtra("contryId");

        initView();
        initData();
        addListener();
    }

    private void initView() {
        adapter = new VisaListAdapter(list,this);
        visaLV.setAdapter(adapter);
    }

    private void initData() {
        x.http().get(getParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                list.addAll(JSONArray.parseArray(s,VisaListBean.class));
                x.image().bind(img,list.get(0).getFlagDir());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void addListener() {

    }

    public RequestParams getParams() {
        RequestParams params = new RequestParams(PATH_VISALIST);
        params.addBodyParameter("action","VisaForCountryID");
        params.addBodyParameter("CountryID",countryID);
        params.addBodyParameter("RetJson","1");
        return params;
    }
}
